package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"
	context2 "kop/framework/context"
	"kop/framework/errors"
	ringC "kop/modules/ring/client"
	"kop/pb"

	captainC "kop/modules/captain/client"
	itemC "kop/modules/item/client"
	roleC "kop/modules/role/client"
	shopC "kop/modules/shop/client"
)

type ShopService struct {
	context2.Context
}

func (s *ShopService) DefaultShop(ctx context.Context, req *wrappers.Int32Value) (*pb.ShopReply, error) {

	var reply pb.ShopReply

	reply.Shop = shopC.GetShop(ctx, req)
	reply.GoodsList = shopC.GetGoodsList(s.GetRoleID(ctx), reply.Shop.ShopID)

	return &reply, nil
}

func (s *ShopService) Buy(ctx context.Context, req *pb.BuyRequest) (*pb.ShopBuyResp, error) {

	return shopC.ShopService.Buy(ctx, req)
}

func (s *ShopService) Refresh(ctx context.Context, req *wrappers.Int32Value) (*pb.ShopRefreshReply, error) {

	var reply pb.ShopRefreshReply

	var roleID = s.GetRoleID(ctx)

	var shop = shopC.GetShop(ctx, req)
	if shop.RefreshNum < 1 {
		return &reply, errors.ParamError
	}

	if shop.RefreshItem != nil {

		var err error
		if reply.Item, err = itemC.DelItem(roleID, s.GetServerID(ctx), shop.RefreshItem.ItemID, shop.RefreshItem.Number); err != nil {
			return &reply, err
		}
	}

	reply.GoodsList = shopC.Refresh(roleID, shop)
	reply.Shop = shop
	return &reply, nil
}

const (
	//GuildShop   = 5
	CaptainShop = 7 // 船长商店ID
	RingShop    = 1
)

func (s *ShopService) CaptainShop(ctx context.Context, _ *empty.Empty) (*pb.ShopReply, error) {

	var reply pb.ShopReply

	reply.Shop = shopC.GetShop(ctx, &wrappers.Int32Value{Value: CaptainShop})
	reply.GoodsList = shopC.GetGoodsList(s.GetRoleID(ctx), reply.Shop.ShopID)

	return &reply, nil
}

// 船长商店购买船长专用接口
func (s *ShopService) BuyCaptain(ctx context.Context, req *wrappers.Int32Value) (*pb.BuyCaptainReply, error) {

	var reply pb.BuyCaptainReply

	var roleID = s.GetRoleID(ctx)

	var goods, err = shopC.GetGoods(roleID, req.Value)
	if err != nil {
		return &reply, err
	}

	var role = roleC.GetRole(roleID)
	if role.Level < goods.Level {
		return &reply, errors.ParamError
	}
	if role.VIP < goods.VIP {
		return &reply, errors.ParamError
	}

	if reply.Item, err = itemC.DelItem(roleID, s.GetServerID(ctx), goods.NeedItem.ItemID, goods.NeedItem.Number); err != nil {
		return &reply, err
	}

	goods.BuyNum -= 1
	shopC.SaveGoods(roleID, goods)

	reply.Goods = goods
	reply.CreateCaptainResp = captainC.CreateCaptain(ctx, goods.PropID)
	return &reply, nil
}

func (s *ShopService) RingShop(ctx context.Context, _ *empty.Empty) (*pb.ShopReply, error) {

	var reply pb.ShopReply

	reply.Shop = shopC.GetShop(ctx, &wrappers.Int32Value{Value: RingShop})
	reply.GoodsList = shopC.GetGoodsList(s.GetRoleID(ctx), reply.Shop.ShopID)

	return &reply, nil
}

func (s *ShopService) BuyRing(ctx context.Context, req *wrappers.Int32Value) (*pb.BuyRingReply, error) {

	var reply pb.BuyRingReply
	var roleID = s.GetRoleID(ctx)

	var goods, err = shopC.GetGoods(roleID, req.Value)
	if err != nil {
		return &reply, err
	}

	var role = roleC.GetRole(roleID)
	if role.Level < goods.Level {
		return &reply, errors.ParamError
	}
	if role.VIP < goods.VIP {
		return &reply, errors.ParamError
	}

	if reply.Item, err = itemC.DelItem(roleID, s.GetServerID(ctx), goods.NeedItem.ItemID, goods.NeedItem.Number); err != nil {
		return &reply, err
	}

	goods.BuyNum -= 1
	shopC.SaveGoods(roleID, goods)

	reply.Goods = goods
	reply.Ring = ringC.Create(roleID, goods.PropID)
	return &reply, nil
}
